package Chapter05;

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.io.*;

/**
 * Created by a on 2017/6/10.
 */
public class Test114 {
    public static void main(String[] args) {
        Login login = new Login("mingrisoft","mr");
        System.out.println("输出原始对象的信息");
        System.out.println(login);
        System.out.println("输出克隆对象的信息");
        try {
            Login login2 = login.clone();
            System.out.println(login2);
            System.out.println(login2.getPassword());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

class Login implements Serializable {
    private static final long seriaVersionUID = 1234343555344L;
    private static String username;
    private static String password;

    public Login(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public static long getSeriaVersionUID() {
        return seriaVersionUID;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();
        sb.append("用户名:"+username+",");
        sb.append("密码:"+password);
        return sb.toString();
    }

    @Override
    public Login clone() throws CloneNotSupportedException {
        Login login = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(this);
            oos.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        try {
            ObjectInputStream ois = new ObjectInputStream(bais);
            login = (Login)ois.readObject();
            ois.close();
        }catch (IOException e){
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return login;
    }
}